from fastapi import APIRouter

from app.database.postgre import SessionDep
from app.models.common import AppIdJSON, AppIdParams
from app.models.apps import (MyAppListParams, CreateAppJSON, UpdateAppJSON, BatchDeleteAppsJSON,
                             GetLoginTimesParams)
from app.services import accounts, apps

router = APIRouter(
    prefix='/api/v1/apps',
    tags=['backend', 'apps']
)


@router.get('/list')
async def apps_list(account: accounts.LoggedInAccount, query_params: MyAppListParams, session: SessionDep):
    response = await apps.get_apps_list(account, query_params, session)
    return response


@router.post('/create')
async def create_app(account: accounts.LoggedInAccount, json_data: CreateAppJSON, session: SessionDep):
    response = await apps.create_new_app(account, json_data, session)
    return response


@router.get('/info')
async def app_info(account: accounts.LoggedInAccount, query_params: AppIdParams, session: SessionDep):
    response = await apps.get_app_info(account, query_params, session)
    return response


@router.post('/update')
async def update_app(account: accounts.LoggedInAccount, json_data: UpdateAppJSON, session: SessionDep):
    response = await apps.update_app_by_id(account, json_data, session)
    return response


@router.post('/delete')
async def delete_app(account: accounts.LoggedInAccount, json_data: AppIdJSON, session: SessionDep):
    response = await apps.delete_app_by_id(account, json_data, session)
    return response


@router.post('/batch_delete')
async def batch_delete_apps(account: accounts.LoggedInAccount, json_data: BatchDeleteAppsJSON, session: SessionDep):
    response = await apps.delete_app_by_ids(account, json_data, session)
    return response


@router.post('/generate_fk')
async def generate_flag_key(account: accounts.LoggedInAccount, json_data: AppIdJSON, session: SessionDep):
    response = await apps.create_app_flag_key(account, json_data, session)
    return response


@router.get('/login_times')
async def app_login_times(account: accounts.LoggedInAccount, query_params: GetLoginTimesParams, session: SessionDep):
    response = await apps.get_app_login_times(account, query_params, session)
    return response
